package com.xxyf.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxyf.entity.Username;
import com.xxyf.mapper.MenuMapper;
import com.xxyf.mapper.UsernameMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author 小小怡飞
 * @Date 2022/12/3 14:41
 * @Version JDK 8
 */
@Service
public class LoginDetailsServerImp implements UserDetailsService {
    @Autowired
    private UsernameMapper userMapper;
    @Autowired
    private MenuMapper mapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<Username> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Username::getAccount,username);
//        查询用户
        Username user = userMapper.selectOne(lambdaQueryWrapper);
        if (Objects.isNull(user)){
            throw new RuntimeException("用户名错误");
        }
        List<String> list = mapper.permissionsByUserId(user.getId());
//        封装返回
        return new LogUserDetaImp(user,list);
    }
}
